package com.bocai.grainshop.order.listener;

import com.bocai.grainshop.order.entity.OrderEntity;
import com.bocai.grainshop.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    /**
     * @Author: SunJinbo
     * @Description  延时队列 定时关单
     * @Param []
     * @return void
     * @Data Created in 2022/4/25 18:24
     */
    @RabbitHandler
    public void OrderClose(OrderEntity order, Message message, Channel channel) throws IOException {
        log.info("rabbitMq接受到的订单信息-》{}"+order.toString());
        try {
            orderService.OrderClose(order);
            //TODO 关单完成 手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //TODO 发生异常 拒签
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
